/**
 * Created by mei on 15-1-5.
 */
//记录上一次操作的表单
var lastItem = 1;
var itemCount = 2;
//保存表单数据(暂时只用来展示作用 -20150109修订, 定为存储新闻id)
var formArray = new Array();
var postIdArray = new Array();
//标记表单id与位置关系的抛锚数组
var flagIndexArray = new Array();

//建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
var ue = UE.getEditor('editor');

$(function () {

    //默认先显示编辑图文界面
    showInnerEditor();

    //初始化
    flagIndexArray.push(1);
    //编辑按钮
    $('#editIcon' + 1).click(function () {
        var dataId = $(this).data('editid');
        saveFormItem(lastItem, formArray);

        showInnerEditor();
        moveFormItem(dataId, formArray, flagIndexArray, false, true);
    });

    //"读取列表"按钮
    $('#readListIcon' + 1).click(function () {
        var dataId = $(this).data('listid');
        showInnerNewsList();
    });

    //添加表单的操作
    $('#js_add_appmsg').click(function () {
        //记录下当前位置
        flagIndexArray.push(itemCount);
        var itemId = 'appmsgItem' + itemCount;
        $('#js_appmsg_preview').append("<div name=\"appmsgItem\" id=\"" + itemId + "\" data-fileid=\"202331706\" data-id=\"" + itemCount + "\"\n" +
            "                                 class=\"appmsg_item js_appmsg_item has_thumb\">\n" +
            "                                <img id=\"appmsg_img" + itemCount + "\" class=\"js_appmsg_thumb appmsg_thumb\" src=>\n" +
            "                                <i class=\"appmsg_thumb default\">缩略图</i>\n" +
            "                                <h4 class=\"appmsg_title\"><a  id=\"appmsg_title" + itemCount + "\" onclick=\"return false;\" href=\"javascript:void(0);\"\n" +
            "                                                            target=\"_blank\">标题</a></h4>\n" +
            "\n" +
            "                                <div class=\"appmsg_edit_mask\">\n" +
            "                                    <a id=\"editIcon" + itemCount + "\" class=\"icon18_common edit_gray js_edit\" data-editid=\"" + itemCount + "\" onclick=\"return false;\"\n" +
            "                                       href=\"javascript:void(0);\">编辑</a>\n" +

            "                                    <a id=\"delIcon" + itemCount + "\" class=\"icon18_common del_gray js_del\" data-deleid=\"" + itemCount + "\" onclick=\"return false;\"\n" +
            "                                       href=\"javascript:void(0);\">删除</a>\n" +
            "                                </div>\n" +
            "                            </div>");
        /*            "                                    <a id=\"readListIcon" + itemCount + "\" class=\"icon14_common sort_gray js_read\" data-listid=\"" + itemCount + "\" onclick=\"return false;\"\n" +
         "                                       href=\"javascript:void(0);\">从列表读取</a>\n" +*/
//        setEditIcon(itemCount,lastItem, formArray, flagIndexArray);
        //编辑按钮
        $('#editIcon' + itemCount).click(function () {
            var dataId = $(this).data('editid');
            saveFormItem(lastItem, formArray);
//            lastItem = dataId;
            showInnerEditor();
            moveFormItem(dataId, formArray, flagIndexArray, false, true);
        });

        //删除按钮
        $('#delIcon' + itemCount).click(function () {
            var dataId = $(this).data('deleid');
            $('#appmsgItem' + dataId).remove();

            //清除数组内的数据
            var index = flagIndexArray.indexOf(dataId);
            var edtingIndex = flagIndexArray.indexOf(lastItem);
            var shouldReread = false;
            if (edtingIndex == index)
                shouldReread = true;
            flagIndexArray.splice(index, 1);

            delete formArray[dataId];
            delete postIdArray[dataId];

            var moveIndex;
            //计算出下个抛锚的位置

            if (shouldReread) {
                if (index < flagIndexArray.length) {
                    moveIndex = index;                //此时index相对应刚刚已经前进一步
                } else {
                    moveIndex = 0;
                }
            }
            else {
                moveIndex = flagIndexArray.indexOf(lastItem);
            }

            moveFormItem(moveIndex, formArray, flagIndexArray, true, shouldReread)
        });

        itemCount++;
    });

    //输入框监听
    $('.js_title').on('input', function () {
        var content = $(this).val();
        if ('' == content) {
            content = '标题';
        }
        $('#appmsg_title' + lastItem).text(content);
    });




    /**点击modal选择后的事件*/
    $('#add-return').click(function () {
        //把标题, 作者等信息填充到界面中展示
        var title = "金正恩刺杀";
        var js_author = "金正恩";
        var imgSrc = "http://markdown-dasunsy.qiniudn.com/`W0W79PA5$_Y)P(2ZW[~`[U.jpg";
        var content = "都说要刺杀金正恩了";
        //展示出界面
        $('.js_title').val(title);
        $('.js_author').val(js_author);
        $('#img_cover').attr('src', imgSrc);
        $("#appmsg_img" + lastItem).attr("src", imgSrc);
        ue.setContent(content);
        //保存新闻id到数组
        postIdArray[lastItem] = lastItem;
    });

    //保存操作
    $('#js_submit').click(function () {
        //提交数组表单等操作
        alert(postIdArray.toString());
    });

    /*********************************被抛弃的分割线 start **********************************/
    $("#cover_input").change(function () {
        var objUrl = getObjectURL(this.files[0]);
        alert(this.files);
        console.log("objUrl = " + objUrl);
        if (objUrl) {
            $("#img_cover").attr("src", objUrl);
            $("#appmsg_img" + lastItem).attr("src", objUrl);
        }
    });


    //"读取列表"按钮
    $('#readListIcon' + itemCount).click(function () {
        var dataId = $(this).data('listid');
        showInnerNewsList();
    });


    /**建立一個可存取到該file的url*/
    function getObjectURL(file) {
        var url = null;
        if (window.createObjectURL != undefined) { // basic
            url = window.createObjectURL(file);
        } else if (window.URL != undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }

    //保存操作
    $('#js_submit').click(function () {
        
    //删除操作
    $('.js_removeCover').click(function () {
        $("#img_cover").attr("src", '');
        $("#appmsg_img" + lastItem).attr("src", '');
        $('#cover_input').attr('value', "");
    });
    /*********************************被抛弃的分割线 end **********************************/
})
;

/**移动并且获取表单数据*/
function moveFormItem(data, formArray, flagIndexArray, isIndex, shouldRead) {
    var dataId;
    var index;
    if (isIndex) {
        index = data;
        dataId = flagIndexArray[index];
    } else {
        dataId = data;
        index = flagIndexArray.indexOf(dataId);
    }
    lastItem = dataId;

    if (shouldRead)
        readFormItem(dataId, formArray);
    var height = 0;
    if (1 == index)
        height = 198 + 'px';
    else if (1 < index)
        height = (198 + 119 * (index - 1)) + 'px';
    $('.appmsg_editor').css("margin-top", height);
}


/**保存表单*/
function saveFormItem(itemId, formArray) {
    var title = $('.js_title').val();
    var author = $('.js_author').val();
    var imgSrc = $('#img_cover').attr('src');
    var inputFileSrc = $('#cover_input').val();
    var itemArray = new Array();
    itemArray['title'] = title;
    itemArray['author'] = author;
    itemArray['imgSrc'] = imgSrc;
    itemArray['inputFileSrc'] = inputFileSrc;

    formArray[itemId] = itemArray;
}

/**读取订单内容并且填充*/
function readFormItem(itemId, formArray) {
    var title = '';
    var author = '';
    var imgSrc = '';
    var inputFileSrc = '';

    if (formArray.hasOwnProperty(itemId)) {
        var itemArray = formArray[itemId];
        title = itemArray['title'];
        author = itemArray['author'];
        imgSrc = itemArray['imgSrc'];
        inputFileSrc = itemArray['inputFileSrc'];
    }
    $('.js_title').val(title);
    $('.js_author').val(author);
    $('#img_cover').attr('src', imgSrc);
    $('#cover_input').val(inputFileSrc);

    if ('' == title)
        $('#appmsg_title' + itemId).text('标题');
    else
        $('#appmsg_title' + itemId).text(title);
}

function showInnerEditor() {
    $('.inner').show();
    $('.inner_news_list').hide();
}

function showInnerNewsList() {
    $('.inner_news_list').show();
    $('.inner').hide();
}


